package com.master.service.repair;

import com.master.constant.constant.Householder;
import com.master.constant.model.CsHouseholder;
import com.master.constant.model.CsRepair;
import com.master.controller.repair.CsRepairVO;
import com.master.mapper.modelMapper.CsHouseholderDao;
import com.master.mapper.modelMapper.CsRepairDao;
import com.master.common.baseService.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname SysRepairServiceImpl
 * @description TODO
 * @Date 2021/5/31 13:57
 **/
@Service
public class CsRepairServiceImpl extends BaseServiceImpl implements CsRepairService {

    @Autowired
    private CsRepairDao csRepairDao;

    @Autowired
    private CsHouseholderDao csHouseholderDao;


    @Override
    public List<Map<String, Object>> getRepairsByCondition(Map<String, Object> condition) {
        List<Map<String, Object>> repairs = new ArrayList<>();
        try {
            repairs = csRepairDao.getRepairsByCondition(condition);
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }

        return repairs;
    }

    @Override
    public Boolean addRepairs(List<CsRepair> repairs) {
        CsRepair repair = repairs.get(0);

        //获取住户信息
        Map<String, Object> condition = new HashMap<>();
        condition.put(Householder.HOUSEHOLDER_NO, repair.getRepairHouseholder());
        CsHouseholder householder = (CsHouseholder) csHouseholderDao.getHouseholderByCondition(condition).get(0);

        repair.setBuildingId(householder.getBuildingId());
        repair.setHouseId(householder.getHouseId());

        boolean mark = false;
        try {
            csRepairDao.insertSelective(repair);
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }
        return mark;
    }

    @Override
    public Boolean modifyRepairs(CsRepair repair) {
        boolean mark = false;

        //修改维修单信息
        try {
            //修改维修单信息
            csRepairDao.updateByPrimaryKeySelective(repair);

            //判断是否收费，创建费用单
            if (Objects.equals(repair.getCharge(), CsRepairConstant.CHARGE_ON)){
                
            }

            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }
        return mark;
    }

    @Override
    public Boolean allocateRepairs(CsRepairVO csRepairVO) {
        boolean mark = false;
        List<Long> repairIds = csRepairVO.getRepairIds();
        Map<String, Object> coordinateParams = csRepairVO.getCoordinateParams();

        //批量修改维修单信息
        try {
            csRepairDao.allocateRepairs(repairIds, coordinateParams);
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }
        return mark;
    }

    @Override
    public Boolean deleteRoleByRepairId(Long repairId) {
        boolean mark = false;
        return mark;
    }

    @Override
    public Boolean deleteRolesByRepairIds(List<Long> repairIds) {
        boolean mark = false;
        return mark;
    }
}
